﻿<?php 

$ra=require_once './lib/mysql.class.php';
require_once './lib/Http.php';

class _Db
{
    protected static $db;
    protected static $conf = [
        'host' => '127.0.0.1',
        'port' => 3306,
        'user' => 'root',
        'passwd' => 'Abc!123Abc',
        'dbname' => 'fastadmin',
        'prefix' => 'fa_'
    ];
    protected static $instance;

    public static function instance()
    {
        if (isset(self::$instance)) {
            return self::$instance;
        } else {
            return self::$instance = new MMysql(self::$conf);
        }
    }
}
// var_dump(_Db::instance());
/**
         * http://data.gate.io/api2/1/ticker/btc_usdt中国  取last
         * https://bittrex.com/api/v1.1/public/getmarketsummary?market=usdt-btc美国  取Last
         * https://api.bithumb.com/public/ticker/btc 韩国    取closing_price
         * https://api.bitfinex.com/v1/pubticker/btcusd   香港  取last_price
         * https://api.bitflyer.jp/v1/ticker/ 日本   取ltp
         */
        $huilv = Http::get('https://api.fixer.io/latest?base=USD');
        $huilv_arr = json_decode($huilv, true);
        $c_last = Http::get('http://data.gate.io/api2/1/ticker/btc_usdt');
        $c_last_arr = json_decode($c_last, true);
        if ($c_last_arr['last'] == 0) {
            return $this->handelData();
        }
        $data['c_last'] = (double)sprintf("%1\$.2f", $c_last_arr['last']);   //中国

        $u_last = Http::get('https://bittrex.com/api/v1.1/public/getmarketsummary?market=usdt-btc');//美国
        $u_last_arr = json_decode($u_last, true);
        if ($u_last_arr['result'][0]['Last'] == 0) {
            return $this->handelData();
        }
        $data['u_last'] = (double)sprintf("%1\$.2f", $u_last_arr['result'][0]['Last']);

        $closing_pric = Http::get('https://api.bithumb.com/public/ticker/btc');//韩国
        $closing_pric_arr = json_decode($closing_pric, true);
        if ($closing_pric_arr['data']['closing_price'] == 0) {
            return $this->handelData();
        }
        $data['closing_price'] = (double)sprintf("%1\$.2f", $closing_pric_arr['data']['closing_price'] / $huilv_arr['rates']['KRW']);

        $last_price = Http::get('https://api.bitfinex.com/v1/pubticker/btcusd');//香港
        $last_price_arr = json_decode($last_price, true);
        if ($last_price_arr['last_price'] == 0) {
            return $this->handelData();
        }
        $data['last_price'] = (double)number_format($last_price_arr['last_price'], 2, '.', '');;

        $ltp = Http::get('https://api.bitflyer.jp/v1/ticker/'); // 日本
        $ltp_arr = json_decode($ltp, true);
        if ($ltp_arr['ltp'] == 0) {
            return $this->handelData();
        }
        $data['ltp'] = (double)sprintf("%1\$.2f", $ltp_arr['ltp'] / $huilv_arr['rates']['JPY']);
        $data['create_time'] = time();
      
		$res = _Db::instance()->insert("huobi", $data);
		if($res){
			echo '请求成功';
			logger('请求成功'); 
		}else{
			echo '请求失败';
			logger('请求失败');
		}
			
		
		function logger($log_content)
        {
            $max_size = 10000;
            $log_filename =  __DIR__."../log.xml";

            if (file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)) {
                unlink($log_filename);
            }
            file_put_contents($log_filename, date('H:i:s') . " " . $log_content . "\r\n", FILE_APPEND);
       }

?>